const app = getApp();
var ea = require('../../utils/each.js');
Page({
  data: {
    quanbu_page:1,
    daifukuan_page:1,
    daifahuo_page:1,
    daisouhuo_page:1,
    yiwancheng_page:1,
    getdingdanUrl: app.data.url + '/api/my/getdingdan',
    getquerenUrl:app.data.url +'/api/my/queren',
    tables:[
      {
        'name':"全部",
        'id':0
      },
      {
        'name': "待付款",
        'id': 1
      },
      {
        'name': "待发货",
        'id': 2
      },
      {
        'name': "待收货",
        'id': 3
      },
      {
        'name': "已完成",
        'id': 4
      },
    ],
    shuju:[[],[],[],[],[]],
    indexs:0

  },
  aa: function (e) {
    var indexs = e.currentTarget.dataset.index;
    this.setData({indexs:indexs});
  },
  // ab: function () {
  //   wx.navigateTo({
  //     url: '/pages/daifukuan/daifukuan',
  //   })
  // },
  // ac: function () {
  //   wx.navigateTo({
  //     url: '/pages/daifahuo/daifahuo',
  //   })
  // },
  // ad: function () {
  //   wx.navigateTo({
  //     url: '/pages/daishouhuo/daishouhuo',
  //   })
  // },
  tiaozhuan: function () {
    wx.switchTab({
      url: '/pages/jiangpin/jiangpin',

    })
  },

  queren:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '你确定已经收货了吗？',
      success:function(re){
        if (re.confirm){
          var user = wx.getStorageSync('user');
          wx.request({
            url: that.data.getquerenUrl,
            data:{token:user.token,id:id},
            method:'post',
            success:function(res){
              var data = res.data;
              console.log(data);
              // return;
              if(data.code==200){
                wx.showToast({
                  title: '收货完成',
                })
                setTimeout(function(){
                  that.onPullDownRefresh();
                },500)
                // var shuju = that.data.shuju;
                // var shuju4 = shuju[4];
                // var newshuju = [];
                // ea.each(shuju[3], function (i, v) {
                //   if (v.id == id) {
                //     v.order_status = '已完成';
                //     shuju4.push(v);
                //   } else {
                //     newshuju.push(v);
                //   }
                // })
                // shuju[3] = newshuju;
                // shuju[4] = shuju4;
                
                // that.setData({ shuju: shuju });
              }else{
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
              }
              }
          })
        }
      }
    })

  },


/**
 * 全部下拉
 */
  quanbuData:function(){
    var page = this.data.quanbu_page;
    // console.log(this);
    if(page==0){
      wx.showToast({
        title: '没有更多数据了',
        icon:'none'
      })
      return;
    }
    console.log(page);
    var that = this;
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getdingdanUrl,
      data:{token:user.token,page:page},
      method:'post',
      success:function(res){
        var data = res.data;
        console.log(data);
        if(data.code==200){
          var shuju = that.data.shuju;
          var newshuju = shuju[0];
          var array = data.data.data;
          console.log(array);
          // return
          that.setData({quanbu_page:page+1});
          if(array.length<10){
            that.setData({quanbu_page:0});
          }
          ea.each(array, function (i, v) {
                newshuju.push(v);
          })
          shuju[0] = newshuju;
          that.setData({shuju:shuju});
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        } 
        wx.hideLoading();
      }
    })
  },

/**
 * 待付款下拉
 */
  daifukuanData:function(){
    var page = this.data.daifukuan_page;
    // console.log(page);
    if (page == 0) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return;
    }
    var that = this;
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getdingdanUrl,
      data: { token: user.token ,type:1,page:page},
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.code == 200) {
          var shuju = that.data.shuju;
          var newshuju = shuju[1];
          var array = data.data.data;
          that.setData({ daifukuan_page: page + 1 });
          if (array.length < 10) {
            that.setData({ daifukuan_page: 0 });
          }
          ea.each(array, function (i, v) {
            newshuju.push(v);
          })
          shuju[1] = newshuju;
          console.log(shuju);
          that.setData({ shuju: shuju });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
        wx.hideLoading();
      }
    })
  },

/**
 * 待发货下拉
 */
  daifahuoData: function () {
    var page = this.data.daifahuo_page;
    console.log(this);
    if (page == 0) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return;
    }
    var that = this;
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getdingdanUrl,
      data: { token: user.token ,type:2,page:page},
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.code == 200) {
          var shuju = that.data.shuju;
          var newshuju = shuju[2];
          var array = data.data.data;
          that.setData({ daifahuo_page: page + 1 });
          if (array.length < 10) {
            that.setData({ daifahuo_page: 0 });
          }
          ea.each(array, function (i, v) {
            newshuju.push(v);
          })
          shuju[2] = newshuju;
          console.log(shuju[2]);
          that.setData({ shuju: shuju });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
        wx.hideLoading();
      }
    })
  },

/**
 * 待收货下拉
 */
  daishouhuoData: function () {
    var page = this.data.daisouhuo_page;
    if (page == 0) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return;
    }
    var that = this;
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getdingdanUrl,
      data: { token: user.token ,type:3,page:page},
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.code == 200) {
          var shuju = that.data.shuju;
          var newshuju = shuju[3];
          var array = data.data.data;
          that.setData({ daisouhuo_page: page + 1 });
          if (array.length < 10) {
            that.setData({ daisouhuo_page: 0 });
          }
          ea.each(array, function (i, v) {
            newshuju.push(v);
          })
          shuju[3] = newshuju;
          that.setData({ shuju: shuju });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
        wx.hideLoading();
      }
    })
  },

/**
 * 已完成下拉
 */
  yiwanchengoData: function () {
    var page = this.data.yiwancheng_page;
    if (page == 0) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return;
    }
    var that = this;
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getdingdanUrl,
      data: { token: user.token ,type:4,page:page},
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(res);
        if (data.code == 200) {
          var shuju = that.data.shuju;
          var newshuju = shuju[4];
          var array = data.data.data;
          that.setData({ yiwancheng_page: page + 1 });
          if (array.length < 10) {
            that.setData({ yiwancheng_page: 0 });
          }
          ea.each(array, function (i, v) {
            newshuju.push(v);
          })
          shuju[4] = newshuju;
          that.setData({ shuju: shuju });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
        wx.hideLoading();
      }
    })
  },


  onLoad: function (options) {
    var indexs = options.indexs;
    if(indexs){
      this.setData({indexs:indexs})
    }
    this.quanbuData();
    this.daifukuanData();
    this.daifahuoData();
    this.daishouhuoData();
    this.yiwanchengoData();
    wx.stopPullDownRefresh({
      success: function () {
        console.log('返回了')
      }
    });
  },
onReachBottom:function(){
  var indexs = this.data.indexs;
  if(indexs==0){
    this.quanbuData();
  }else if(indexs==1){
    this.daifukuanData();
  }else if(indexs==2){
    this.daifahuoData();
  }else if(indexs==3){
this.daishouhuoData();
  }else if(indexs==4){
this.yiwanchengoData();
  }
}
  , onPullDownRefresh: function () {
    var indexs = this.data.indexs;
    this.setData({ page: 1,indexs:0,quanbu_page:1,daifahuo_page:1,daifukuan_page:1,daisouhuo_page:1,yiwancheng_page:1});
    this.setData({ shuju: [[],[],[],[],[]] });
    this.onLoad({ indexs: indexs});
  },

  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})


